package net.xdclass.video.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.video.common.Result;
import net.xdclass.video.crawler.utils.StringUtils;
import net.xdclass.video.entity.Movie;
import net.xdclass.video.mapper.MovieMapper;
import net.xdclass.video.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class MovieController {

    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieMapper movieMapper;

    @GetMapping("/apiOne/movie/page")
    public Result page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) String year) {

        QueryWrapper<Movie> movieQueryWrapper = new QueryWrapper<>();
        movieQueryWrapper.orderByDesc("id");


        // 处理类型
        if (StringUtils.isNotBlank(type) && !"全部".equals(type)) {
            String result = type.replace("片", ""); // 去掉“片”
            movieQueryWrapper.like("type", result); // 使用模糊查询
        }

        // 处理年份
        if (StringUtils.isNotBlank(year) && !"全部".equals(year)) {
            movieQueryWrapper.like("year", year);
        }

        // 处理国家
        if (StringUtils.isNotBlank(country) && !"全部".equals(country)) {
            movieQueryWrapper.like("country", country);
        }

        // 处理剧情类型
        if (StringUtils.isNotBlank(genre) && !"全部".equals(genre)) {
            movieQueryWrapper.like("genre", genre); // 确认 genre 字段是否正确
        }
        // 分页查询
        Page<Movie> page = movieService.page(new Page<>(pageNum, pageSize), movieQueryWrapper);
        return Result.success(page);
    }

    @GetMapping("/apiOne/movie/finAll")
    public Result finAll(){
        List<Movie> movie=movieService.list();
        return Result.success(movie);
    }}
